/**
 * ECP Web开发 通用业务实体选择控件.
 * Copyright(c) 2012-2020 YGSoft.Inc
 * @author zhengxianwei 
 * @date 2016-07-04
 */
define(["jquery","ecp.service","ecp.utils","ecp.component.treeSelect","ecp.utils.i18n"],function($,ecp,utils,$treeselect,i18n){
	/**
	 * 声明BisComboBox对象.
	 * @param {Object} origControl 控件的jQuery对象，即占位符。
	 * @param {Object} option 控件的属性对象
	 */
	var BisTreeSelect = function(control,option){
		if(!control || control.length === 0){return;}
		//初始化
		this.init(control,option);
		//渲染控件
		this._render();
	}
	/**
	 * 通过原型定义相关方法，比如渲染、赋值等等.
	 */
	BisTreeSelect.prototype = {
			/**
			 * 初始化.
			 * @param {Object} control 控件的jQuery对象，即占位符。
	    	 * @param {Object} option 控件的属性对象
			 */
			init: function(control, option){
				this.control = control;
				var dOpt = this.control.attr("data-option");
				if(dOpt && dOpt.length !== 0){
					dOpt = $.parseJSON(dOpt);
				}else{
					dOpt = {};
				}
				this.option = $.extend(true,{},option,dOpt);
				delete this.option.data;
			    //下拉数据回调函数
				this.option.listDatas = $.proxy(this._getListDatas,this);
				//树数据回调函数
				this.option.treeDatas = $.proxy(this._getTreeDatas,this);
				//树的懒加载回调函数
				this.option.treeLazyDatas = $.proxy(this._getTreeLazyDatas,this);
			},
			
			/**
	    	 * 渲染.
	    	 */
			_render: function(){
				this.treeSelect = this.control.treeSelect(this.option);
				this.treeSelect.base(this);
	    	},
	    	
	    	/**
	    	 * 获得树选择控件对象.
	    	 */
	    	getTreeSelect: function(){
	    		return this.treeSelect;
	    	},
	    	
	    	/**
	    	 * 获得comboBox对象.
	    	 */
	    	getComboBox: function(){
	    		return this.treeSelect.getComboBox();
	    	},
	    	
	    	/**
	    	 * 继承comboBox表格中渲染须实现的方法.
	    	 * @param {Object} obj 控件对象
	    	 */
	    	inheritGridMethod: function(obj){
	    		if(!obj){return;}
	    		this.treeSelect.inheritGridMethod(obj);
	    	},
	    	/**
	    	 * 获得下拉列表数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 * @param {Object} filter 过滤对象
	    	 */
	    	_getListDatas: function(option, callback, filter){
	    		this._getServer(option, callback, filter);
	    	},
	    	
	    	/**
	    	 * 获得树数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 */
	    	_getTreeDatas: function(option,callback){
	    		this._getServer(option.treeOptions,callback);
	    	},
	    	/**
	    	 * 获得树懒加载数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 */
	    	_getTreeLazyDatas: function(option,callback,filter){
	    		this._getServer(option.treeLazyOptions,callback, filter);
	    	},
	    	/**
	    	 * 获得服务.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 * @param {Object} filter 过滤对象
	    	 */
	    	_getServer: function(option, callback, filter){
	    		if(!option){return;}
	    		var beanId = option.beanId,
	    			method = option.method,
	    			needContext = option.needContext,
	    			pageSize = option.pageSize,
					requestTyle = option.requestType,
	    			param = option.param;
	    		if(!beanId || !method){
	    			window.console.error(i18n.select.paramNotmatch);
	    			return;
	    		}
				if(!requestTyle){
					requestTyle = "GET";
				}
	    		if(!param){
					param = [];
				}
	    		var p = [];
	    		if(needContext !== false){
	    			p.push(ecp.DataContextUtil.getEcpDataContext());
	    		}
	    		if(filter){
	    			if(pageSize){
	    				filter.pageSize = pageSize;
	    			}
	    			p.push(filter);
	    		}
    			param = p.concat(param);
				//获取所有菜单
				var reqMathod = requestTyle == "GET" ? "doGetAsync" : "doPostAsync";
				ecp.RemoteService[reqMathod](beanId,method,param,function(result){
					if(result.isSuccess()){
		            	callback(result.data);
					}else{
						window.console.error("beanId: "+beanId +" method: " +method +" \t\n" + i18n.select.queryError+": "+result.data);
					}
	    		});
	    	}
	};
	
	(function($) {
		var old = $.fn.bisTreeSelect;
		$.fn.bisTreeSelect = function(option){
			var me = this;
			if(!me || me.length === 0){
				if(window.console){
					window.console.error("ecp.component.bisTreeSelect"+i18n.select.renderError);
				}
				return;
			}
			option = option || {};
			var type = typeof(option);
			//如果第一个参数传入的是对象或空则初始化空件，如果传入的是字符串，则认为是调用里面的内部方法。
			if (typeof(option) === "object") {
				var btss = [];
				this.each(function() {
					var instanceOptions = $.extend({}, option, true);
					var instance = new BisTreeSelect($(this), instanceOptions);
					this.bisTreeSelect = instance;
					btss.push(instance);
				});
				if(btss.length === 1){
					btss = btss[0];
				}
				return btss;
			} else if (type === "string") {
				var args = Array.prototype.slice.call(arguments, 1);
				if (BisTreeSelect.prototype[option]) {
					return BisTreeSelect.prototype[option].call(args);
				}
			} else if (option === true) {
				return me[0] && me[0].bisTreeSelect;
			}
		};
		$.fn.bisTreeSelect.Constructor = BisTreeSelect;
		$.fn.bisTreeSelect.noConflict = function() {
			$.fn.bisTreeSelect = old;
			return this;
		};
	}(jQuery));
	return BisTreeSelect;
});